package com.example.emos.workflow.task;

import cn.hutool.core.map.MapUtil;
import com.example.emos.workflow.db.dao.TbUserDao;
import com.example.emos.workflow.service.MeetingService;
import com.example.emos.workflow.service.ReimService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Yanx
 * @Create 2022-05-18-16:02
 * 给审批者发短信 提醒 审批任务
 */
@Component
@Slf4j
public class SendEmailTask {

    @Autowired
    private TbUserDao userDao;

    @Autowired
    private MeetingService meetingService;


    @Autowired
    private ReimService reimService;
    @Autowired
    private EmailTask emailTask;

    public void SendEmailToApproval(Map map) {


        String type = MapUtil.getStr(map, "type");
//        String identity = MapUtil.getStr(map, "identity");
//        Integer managerId = MapUtil.getInt(map, "managerId");
//        Integer gmId = MapUtil.getInt(map, "gmId");
//        String sameDept = MapUtil.getStr(map, "sameDept");

        String emailTitle;
        String emailText;
        SimpleMailMessage message = new SimpleMailMessage();

        switch (type) {
            case "会议申请":
                if ("同意".equals(MapUtil.getStr(map, "result"))) {
                    List<Integer> integers = meetingService.searchMeetingMembersByUUID(MapUtil.getStr(map, "uuid"));
                    //全部会议人员id
                    Integer[] members = integers.toArray(new Integer[integers.size()]);
                    //员工姓名
                    // ArrayList<String> names = userDao.searchNameByIds(members);


                    emailTitle = MapUtil.getStr(map, "creatorName") + "的会议通知";
                    emailText = "你好，你在 " + MapUtil.getStr(map, "date") + " " + MapUtil.getStr(map, "start")
                            + "至" + MapUtil.getStr(map, "end")
                            + "有一场主题为:" + MapUtil.getStr(map, "title") + " 的"
                            + MapUtil.getStr(map, "meetingType") + "，请按时参加会议!";

                    ArrayList<String> emails = userDao.searchEmailByIds(members);
                    for (String email : emails) {
                        message.setSubject(emailTitle);
                        message.setText(emailText);
                        message.setTo(email);
                        emailTask.sendAsync(message);
                    }
                } else {
                    Integer[] createId = new Integer[]{MapUtil.getInt(map, "creatorId")};
                    String email = userDao.searchEmailByIds(createId).get(0);
                    emailTitle = MapUtil.getStr(map, "creatorName") + "的会议审批结果";
                    emailText = "你好，你在 " + MapUtil.getStr(map, "date") + " " + MapUtil.getStr(map, "start")
                            + "至" + MapUtil.getStr(map, "end")
                            + "有一场主题为:" + MapUtil.getStr(map, "title") + " 的"
                            + MapUtil.getStr(map, "meetingType")
                            + "，没有通过审批！具体原因请发邮件给相关领导咨询。";
                    message.setSubject(emailTitle);
                    message.setText(emailText);
                    message.setTo(email);
                    emailTask.sendAsync(message);
                }
                break;
            case "会议开始提醒邮件":
                /*********/

                List<Integer> integers = meetingService.searchMeetingMembersByUUID(MapUtil.getStr(map, "uuid"));
                //全部会议人员id
                Integer[] members = integers.toArray(new Integer[integers.size()]);
                //员工姓名
                // ArrayList<String> names = userDao.searchNameByIds(members);


                emailTitle = MapUtil.getStr(map, "creatorName") + "的会议通知";
                emailText = "你好，你在 " + MapUtil.getStr(map, "date") + " " + MapUtil.getStr(map, "start")
                        + "至" + MapUtil.getStr(map, "end")
                        + "有一场主题为:" + MapUtil.getStr(map, "title") + " 的"
                        + MapUtil.getStr(map, "meetingType") + "需要参加，" + "会议号为:"
                        + MapUtil.getInt(map, "roomId")
                        + ",15分钟后就要开始了，请按时参加会议。";

                ArrayList<String> emails = userDao.searchEmailByIds(members);
                for (String email : emails) {
                    message.setSubject(emailTitle);
                    message.setText(emailText);
                    message.setTo(email);
                    emailTask.sendAsync(message);
                }
                break;
            case "报销申请":
                Integer sentEmailTo = MapUtil.getInt(map,"creatorId");

                if ("同意".equals(map.get("result"))) {
                    emailTitle = map.get("title") + "已经被批准";
                    emailText = "你好" + map.get("creatorName") + "，你于" + map.get("createTime")
                            + "的报销申请已经被批准，请及时把报销单签字交给财务归档!";
                } else {
                    emailTitle = map.get("title") + "没有被批准";
                    emailText = "你好" + map.get("creatorName") + "，你于" + map.get("createTime")
                            + "创建的报销申请没有被批准，请找领导核实原因。";
                }
                Integer[] ids = new Integer[]{sentEmailTo};
                ArrayList<String> emails1 = userDao.searchEmailByIds(ids);
                for (String email : emails1) {
                    message.setSubject(emailTitle);
                    message.setText(emailText);
                    message.setTo(email);
                    emailTask.sendAsync(message);
                }
                break;
            case "员工请假":
                if ("同意".equals(MapUtil.getStr(map, "result"))) {
                    emailTitle = MapUtil.getStr(map, "title") + "已经被批准";
                    emailText = "你好" + MapUtil.getStr(map, "creatorName") + "，你于"
                            + MapUtil.getStr(map, "start")
                            + "至" + MapUtil.getStr(map, "end")
                            + "的请假申请已经被批准，请及时把请假单签字交给HR归档!";
                } else {
                    emailTitle = MapUtil.getStr(map, "title") + "没有被批准";
                    emailText = "你好" + MapUtil.getStr(map, "creatorName") + "，你于"
                            + MapUtil.getStr(map, "start")
                            + "至" + MapUtil.getStr(map, "end") + "的请假没有被批准，请找领导核实原因。";
                }
                ids = new Integer[]{MapUtil.getInt(map, "creatorId")};
                emails = userDao.searchEmailByIds(ids);
                for (String email : emails) {
                    message.setSubject(emailTitle);
                    message.setText(emailText);
                    message.setTo(email);
                    emailTask.sendAsync(message);
                }
                break;
            default:
                break;
        }

    }
}


//        HashMap<String, String> map1 = new HashMap<>();
//        map1.put("roleName", "部门经理");
//        map1.put("deptId", "2");
//
//        String emailTitle;
//        String emailText;
//        emailTitle = creatorName + "审批通知";
//        emailText = "你好，你有 " + date + " " + start + "至" + end
//                + "有一场主题为:" + title + " 的" + meetingType + "请及时审批!";
//
//
//        ArrayList<HashMap> list = userDao.searchEmailByRoles(map);
//        list.forEach(one->{
//            String username = (String) one.get("name");//审批者姓名
//            String email = (String) one.get("email");//审批者email
//
//            SimpleMailMessage message = new SimpleMailMessage();//创建邮件消息
//            message.setSubject(emailTitle);
//            message.setText(emailText);
//            message.setTo(email);
//            emailTask.sendAsync(message);
//        });


